package com.igorkundovic.guitarstab.service_hibernate;

import java.io.File;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.igorkundovic.guitarstab.Artist;
import com.igorkundovic.guitarstab.Genre;
import com.igorkundovic.guitarstab.Tab;
import com.igorkundovic.guitarstab.User;
import com.igorkundovic.guitarstab.beans.ArtistBean;
import com.igorkundovic.guitarstab.beans.TabBean;
import com.igorkundovic.guitarstab.service.TabService;



public class TabHibernateService implements TabService{

	
private final Session session;
	
	public TabHibernateService(Session session){
		this.session=session; 
	}
	

	public Long save(Tab tab) {
		Long id=(Long) session.save(tab);
		return id;
	}

	public List<Tab> retrieveMine(User user) {
		List<Tab> tabs=session.createCriteria(TabBean.class).createCriteria("user").add(Restrictions.like("id", user.getId())).list();
		return tabs;
		
	}


	public List<Tab> retrieve() {
			List<Tab> tabs=session.createCriteria(TabBean.class).list();
			return tabs;
	}


	public boolean deleteTab(User user) {
		List<Tab> tabs=retrieveMine(user);
		if(tabs.isEmpty())
			return false;
		for (int i = 0; i < tabs.size(); i++) {
			File file=new File("src\\main\\webapp\\tabs\\" + tabs.get(i).getName());
			file.delete();
			tabs.get(i).setArtist(null);
			tabs.get(i).setGenre(null);
			tabs.get(i).setName(null);
			tabs.get(i).setPath(null);
			tabs.get(i).setUser(null);
			session.delete(tabs.get(i));
		}
		return true;
	}


	public boolean delete(Tab tab) {
		// TODO Auto-generated method stub
		session.delete(tab);
		return true;
	}


	public Tab get(Long id) {
		// TODO Auto-generated method stub
		return (Tab) session.get(TabBean.class, id);
	}


	public void update(Tab tab) {
		// TODO Auto-generated method stub
		Tab tab2 = (Tab) session.merge(tab);
		session.update(tab);
	}


	public List<Tab> searchByArtist(Artist artist,String criteriaQueryExclude) {
		// TODO Auto-generated method stub
		Example example=Example.create(artist).excludeProperty(criteriaQueryExclude).ignoreCase().enableLike(MatchMode.ANYWHERE);
		List<Artist> artists=session.createCriteria(ArtistBean.class).add(example).list();
		List<Tab> tabs=new LinkedList<Tab>();
		Tab artist2;
		for(int i=0;i<artists.size();i++){
		Criteria criteria=session.createCriteria(TabBean.class).createCriteria("artist").add(Restrictions.like("id", artists.get(i).getId()));
		artist2=(Tab) criteria.uniqueResult();
		tabs.add(artist2);
		}
		return tabs;
	}


	public List<Tab> searchBySongName(Artist artist) {
		// TODO Auto-generated method stub
		Example example=Example.create(artist).excludeProperty("name").ignoreCase().enableLike(MatchMode.ANYWHERE);
		List<Artist> artists=session.createCriteria(ArtistBean.class).add(example).list();
		List<Tab> tabs=new LinkedList<Tab>();
		Tab artist2;
		for(int i=0;i<artists.size();i++){
		Criteria criteria=session.createCriteria(TabBean.class).createCriteria("artist").add(Restrictions.like("id", artists.get(i).getId()));
		artist2=(Tab) criteria.uniqueResult();
		tabs.add(artist2);
		}
		return tabs;
	}


	public List<Tab> searchByGenre(Genre genre) {
		// TODO Auto-generated method stub
		TabBean tab=new TabBean();
		tab.setGenre(genre);
		Example example=Example.create(tab).excludeProperty("name").excludeProperty("path").excludeProperty("artist").excludeProperty("user").ignoreCase().enableLike(MatchMode.ANYWHERE);
		List<Tab> artists=session.createCriteria(TabBean.class).add(example).list();
		return artists;
	}


	

	
}
